<template>
  <div class="statistics-container" :style="{background: `url('${bg}')`}">
    <div style="font-size: 16px;padding: 25px; padding-bottom: 0;">活动运营数据</div>
    <div class="dashboard-container">
      <div class="item">
        <div class="number">{{ dashboardData.todayvisitnun }}</div>
        <div class="name">今日活动访问人次</div>
      </div>
      <div class="item">
        <div class="number">{{ dashboardData.totalvisitnum }}</div>
        <div class="name">累计活动访问人次</div>
      </div>

      <div class="item">
        <div class="number">{{ dashboardData.todayticketnum }}</div>
        <div class="name">今日消费券发放数</div>
      </div>
      <div class="item">
        <div class="number">{{ dashboardData.totalticketnum }}</div>
        <div class="name">累计消费券发放数</div>
      </div>

      <div class="item">
        <div class="number">{{ dashboardData.newmember }}</div>
        <div class="name">累计新增会员数</div>
      </div>
      <div class="item">
        <div class="number">{{ dashboardData.totalchecknum }}</div>
        <div class="name">累计消费券核销数</div>
      </div>

      <div class="item">
        <div class="number">{{ dashboardData.totalcheckamount }}</div>
        <div class="name">累计消费券使用金额</div>
      </div>
      <div class="item">
        <div class="number">{{ dashboardData.totalcostamount }}</div>
        <div class="name">累计新增消费金额</div>
      </div>
    </div>
    <div class="chart-container">
      <div class="chart-header">活动趋势</div>
      <div ref="chartRef" style="width: 100%; height: 320px;"></div>
    </div>
  </div>
</template>

<script setup>
import bg from '@/assets/img/bg.png'
import { ref, onMounted, nextTick } from 'vue';
import { useRouter } from 'vue-router';
import { statisticsApi } from '@/api';
import * as echarts from 'echarts';

const router = useRouter();
const dashboardData = ref({});
const chartData = ref([]);

const chartRef = ref(null);
const legendRef = ref(null);
let chartInstance = null;

const renderChart = () => {
  if (!chartRef.value) return;
  if (!chartInstance) {
    chartInstance = echarts.init(chartRef.value);
  }

  // 用 chartData 替代 mockData
  const dataSource = chartData.value && chartData.value.length > 0 ? chartData.value : mockData;

  const days = [...new Set(dataSource.map(item => item.createTime))];
  const types = [...new Set(dataSource.map(item => item.type))];

  const series = types.map(type => ({
    name: type,
    type: 'line',
    data: days.map(day => {
      const found = dataSource.find(item => item.type === type && item.createTime === day);
      return found ? found.dataCount : 0;
    })
  }));

  const option = {
    tooltip: { trigger: 'axis' },
    legend: {
      data: types,
      orient: 'horizontal', // 横向排列
      right: 0,
      top: 0,
      align: 'left'
    },
    grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
    xAxis: { type: 'category', boundaryGap: false, data: days },
    yAxis: { type: 'value' },
    series
  };

  chartInstance.setOption(option);
};

onMounted(() => {
  const uid = localStorage.getItem('uid'); 
  statisticsApi.getDashboard({uid}).then(res => {
    if (res.code === 0) {
      dashboardData.value = res.data[0]
    }
  });

  statisticsApi.getChart({uid}).then(res => {
    if (res.code === 0) {
      chartData.value = res.data;
      renderChart();
    }
  })
  
});

</script>

<style lang="less" scoped>
.statistics-container {
  background-repeat: no-repeat;
  background-size: 100%;
  background-color: #f1f3f4;
  min-height: 100vh;
  padding-bottom: 20px;

  .dashboard-container{
    // 每行两个，白色背景，圆角
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 20px;
    padding-bottom: 0;
    .item {
      width: 48%;
      box-sizing: border-box;
      background-color: white;
      border-radius: 8px;
      font-size: 14px;
      padding: 10px;
      margin-bottom: 15px;
      .number {
        font-size: 24px;
        font-weight: bold;
        color: #f15a4a;
        margin-bottom: 5px;
      }
    }
  }
  .chart-container {
    background: #fff;
    border-radius: 8px;
    margin: 0 20px;
    padding: 10px;
    padding-bottom: 20px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.03);
    position: relative;
    .chart-header {
      font-size: 16px;
      position: absolute;
      left: 10px;
      top: 10px;
    }
  }
}

</style>